//
//  KVBaseTabBarVC.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/18.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

class KVBaseTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let mainTabbar = KVConstemTabbar()
        
        mainTabbar.selectBackBlock = {
            print("这只是一个开始")
            //跳转到发送界面
            let vc = KVComposeViewController()
            self.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
//            self.navigationController?.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
        }
        self.addsubContollers()
        
        //自定义tarbar要通过KVC的方式赋值
        self.setValue(mainTabbar, forKey: "tabBar")
    }
    // MARK - 私有方法
    private func addsubContollers() {
        //加载主页的控制器
        let homeVC = KVHomeTableVC()
        self.loadSubController(homeVC, titleName: "主页", imageName: "tabbar_home")
        
        //加载发现控制器
        let messageVC = KVMessageTableVC()
        self.loadSubController(messageVC, titleName: "消息", imageName: "tabbar_message_center")
        
        //加载发现控制器
        let discoverVC = KVDiscoverTableVC()
        self.loadSubController(discoverVC, titleName: "发现", imageName: "tabbar_discover")
        
      
        
        //加载发现控制器
        let proFileVC = KVProFileTableVC()
        self.loadSubController(proFileVC, titleName: "个人", imageName: "tabbar_profile")
        
    }
    //抽取方法加载
    func loadSubController(controller: UITableViewController,titleName: String,imageName: String) {
        
        
        //设置属性
        controller.title = titleName
        
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置默认情况下的颜色        //取消默认的背景填充
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGrayColor()], forState: UIControlState.Normal)
        
        //设置选中后字体的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        //选中的图片
        let slecectImgName = imageName + "_selected"
        controller.tabBarItem.selectedImage = UIImage(named: slecectImgName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        self.addChildViewController(UINavigationController(rootViewController: controller))
    }

}
